<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>arrayToCSV</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const arrayToCSV = (arr, delimiter = ',') =>
            arr
                .map(i =>
                    i.map(v => (isNaN(v) ? `"${v.replace(/"/g, '""')}"` : v)).join(delimiter)
                )
                .join('\n');
        console.log(arrayToCSV([
            ['a', 'b'],
            ['c', 'd']
        ])); // '"a","b"\n"c","d"'
        console.log(arrayToCSV(
            [
                ['a', 'b'],
                ['c', 'd']
            ],
            ';'
        )); // '"a";"b"\n"c";"d"'
        console.log(arrayToCSV([
            ['a', '"b" great'],
            ['c', 3.1415]
        ])); // '"a","""b"" great"\n"c",3.1415'
    </script>
</body>

</html>